<template>
  <div class="admin-layout">
    <el-container>
      <el-container>
        <el-aside width="200px">
          <el-menu :default-active="activeMenu" router class="el-menu-vertical">
            <el-menu-item index="/admin/user">
              <el-icon><icon-menu /></el-icon>
              <span>用户管理</span>
            </el-menu-item>
            <el-menu-item index="/admin/employee">
              <el-icon><icon-menu /></el-icon>
              <span>员工管理</span>
            </el-menu-item>
            <el-menu-item index="/admin/medicine">
              <el-icon><icon-menu /></el-icon>
              <span>药品信息管理</span>
            </el-menu-item>
            <el-menu-item index="/admin/category">
              <el-icon><icon-menu /></el-icon>
              <span>药品分类管理</span>
            </el-menu-item>
            <el-menu-item index="/admin/stock">
              <el-icon><icon-menu /></el-icon>
              <span>药品库存管理</span>
            </el-menu-item>
          </el-menu>
        </el-aside>

        <el-main>
          <!-- 子路由内容将在这里渲染 -->
          <router-view />
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Menu as IconMenu } from '@element-plus/icons-vue'

const router = useRouter()
const route = useRoute()
const adminInfo = ref({})

// 计算当前激活的菜单项
const activeMenu = computed(() => {
  return route.path
})

// 获取管理员信息
onMounted(() => {
  const storedAdminInfo = localStorage.getItem('userInfo')
  if (storedAdminInfo) {
    adminInfo.value = JSON.parse(storedAdminInfo)
  } else {
    // 如果没有管理员信息，重定向到登录页
    router.push('/login')
  }
})

// 退出登录
const logout = () => {
  ElMessageBox.confirm('确定要退出登录吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  })
    .then(() => {
      // 清除本地存储的管理员信息
      localStorage.removeItem('adminInfo')
      localStorage.removeItem('token')

      // 跳转到登录页
      router.push('/login')
      ElMessage.success('已退出登录')
    })
    .catch(() => {
      // 取消退出
    })
}
</script>
